using micromsg;
using MicroMsg.Common.Algorithm;
using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Protocol;
using MicroMsg.Scene;
using MicroMsg.Storage;
using System;
using System.Collections.Generic;
namespace MicroMsg.Plugin.Sns.Scene
{
	public class NetSceneSnsComment : NetSceneBaseEx<SnsCommentRequest, SnsCommentResponse, SnsCommentRequest.Builder>
	{
		private const string TAG = "NetSceneSnsComment";
		public static Dictionary<string, SnsCommentNeedSend> sendingMap;
		private SnsCommentNeedSend mCommentContent;
		public bool doScene(SnsCommentNeedSend comment)
		{
			if (comment == null || comment.currentComment == null)
			{
				return false;
			}
			if (NetSceneSnsComment.sendingMap != null && NetSceneSnsComment.sendingMap.ContainsKey(comment.strClientID))
			{
				return false;
			}
			this.mCommentContent = comment;
			Account curAccount = AccountMgr.getCurAccount();
			SnsActionGroup.Builder builder = new SnsActionGroup.Builder();
			builder.Id = comment.objectID;
			builder.ParentId = comment.parentID;
			builder.CurrentAction = new SnsAction.Builder
			{
				ToUsername = comment.currentComment.strUserName,
				ToNickname = comment.currentComment.strNickName,
				FromUsername = curAccount.strUsrName,
				FromNickname = curAccount.strNickName,
				Type = comment.currentComment.nType,
				Source = comment.currentComment.nSource,
				CreateTime = comment.currentComment.nCreateTime,
				Content = comment.currentComment.strContent,
				CommentId = comment.currentComment.nCommentId,
				ReplyCommentId = comment.currentComment.nReplyCommentId
			}.Build();
			if (comment.referComment != null)
			{
				builder.ReferAction = new SnsAction.Builder
				{
					ToUsername = comment.referComment.strUserName,
					ToNickname = comment.referComment.strNickName,
					FromUsername = curAccount.strUsrName,
					FromNickname = curAccount.strNickName,
					Type = comment.referComment.nType,
					Source = comment.referComment.nSource,
					CreateTime = comment.referComment.nCreateTime,
					Content = comment.referComment.strContent,
					CommentId = comment.referComment.nCommentId,
					ReplyCommentId = comment.referComment.nReplyCommentId
				}.Build();
			}
			if (NetSceneSnsComment.sendingMap == null)
			{
				NetSceneSnsComment.sendingMap = new Dictionary<string, SnsCommentNeedSend>();
			}
			NetSceneSnsComment.sendingMap.Add(comment.strClientID, comment);
			return this.doSceneEx(builder.Build(), comment.strClientID);
		}
		private bool doSceneEx(SnsActionGroup content, string clientID)
		{
			base.beginBuilder();
			this.mBuilder.BaseRequest = NetSceneBase.makeBaseRequest(0);
			this.mBuilder.Action = content;
			this.mBuilder.ClientId = clientID;
			this.mSessionPack.mCmdID = 100;
			base.endBuilder();
			return true;
		}
		protected override void onFailed(SnsCommentRequest request, SnsCommentResponse response)
		{
			Log.smethod_1("NetSceneSnsComment", "send request failed");
			this.doSceneEnd(request.ClientId, false);
		}
		protected override void onSuccess(SnsCommentRequest request, SnsCommentResponse response)
		{
			RetConst ret = (RetConst)response.BaseResponse.Ret;
			if (ret != RetConst.MM_OK)
			{
				Log.smethod_1("NetSceneSnsComment", "send request failed ret =" + ret.ToString());
				if (ret != RetConst.MMSNS_RET_SPAM && ret != RetConst.MMSNS_RET_BAN && ret != RetConst.MMSNS_RET_PRIVACY && ret != RetConst.MMSNS_RET_COMMENT_HAVE_LIKE && ret != RetConst.MMSNS_RET_COMMENT_NOT_ALLOW && ret != RetConst.MMSNS_RET_CLIENTID_EXIST)
				{
					if (ret != RetConst.MMSNS_RET_COMMENT_PRIVACY)
					{
						this.doSceneEnd(request.ClientId, false);
						return;
					}
				}
				this.doSceneEnd(request.ClientId, true);
				EventCenter.postEvent(EventConst.ON_NETSCENE_SNS_COMMENT_ERR, ret, null);
				return;
			}
			SnsInfoMgr.update(response.SnsObject);
			this.doSceneEnd(request.ClientId, true);
		}
		public void doSceneEnd(string cliendID, bool isSuccess)
		{
			if (NetSceneSnsComment.sendingMap != null)
			{
				NetSceneSnsComment.sendingMap.Remove(cliendID);
			}
			SnsCommentNeedSendMap snsCommentNeedSendMap = ConfigMgr.smethod_0<SnsCommentNeedSendMap>();
			if (snsCommentNeedSendMap == null || snsCommentNeedSendMap.dictionary_0 == null)
			{
				return;
			}
			if (snsCommentNeedSendMap.dictionary_0.get_Count() <= 0)
			{
				return;
			}
			if (isSuccess)
			{
				snsCommentNeedSendMap.dictionary_0.Remove(cliendID);
			}
			else
			{
				SnsCommentNeedSend snsCommentNeedSend = null;
				snsCommentNeedSendMap.dictionary_0.TryGetValue(cliendID, ref snsCommentNeedSend);
				if (snsCommentNeedSend != null)
				{
					snsCommentNeedSend.nRetryTimes++;
				}
			}
			ConfigMgr.write<SnsCommentNeedSendMap>(snsCommentNeedSendMap);
		}
		public static SnsCommentNeedSend creatSnsCommentNeedSend(SnsInfo snsObj, string strContent, CommentType type, AddContactScene source, CommentArg replyInfo = null, int referCommetID = -1)
		{
			SnsCommentNeedSend snsCommentNeedSend = new SnsCommentNeedSend();
			snsCommentNeedSend.objectID = snsObj.nObjectID;
			snsCommentNeedSend.parentID = 0uL;
			snsCommentNeedSend.referComment = null;
			snsCommentNeedSend.strClientID = MD5Core.GetHashString(string.Concat(Util.getNowMilliseconds()));
			snsCommentNeedSend.currentComment = new SnsComment();
			snsCommentNeedSend.currentComment.strContent = strContent;
			snsCommentNeedSend.currentComment.strNickName = snsObj.strNickName;
			snsCommentNeedSend.currentComment.strUserName = snsObj.strUserName;
			if (replyInfo != null)
			{
				snsCommentNeedSend.currentComment.strNickName = replyInfo.strNickName;
				snsCommentNeedSend.currentComment.strUserName = replyInfo.strUserName;
				snsCommentNeedSend.currentComment.nReplyCommentId = replyInfo.commentID;
			}
			snsCommentNeedSend.currentComment.nType = (uint)type;
			snsCommentNeedSend.currentComment.nCreateTime = (uint)Util.getNowSeconds();
			snsCommentNeedSend.currentComment.nSource = (uint)source;
			if (referCommetID >= 0)
			{
				using (List<SnsComment>.Enumerator enumerator = snsObj.commentList.list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						SnsComment current = enumerator.get_Current();
						if (current.nCommentId == referCommetID)
						{
							snsCommentNeedSend.referComment = new SnsComment();
							snsCommentNeedSend.referComment.strContent = current.strContent;
							snsCommentNeedSend.referComment.strNickName = current.strNickName;
							snsCommentNeedSend.referComment.strUserName = current.strUserName;
							snsCommentNeedSend.refFromUserName = snsObj.strUserName;
							snsCommentNeedSend.refFromNickName = snsObj.strNickName;
							snsCommentNeedSend.referComment.nType = current.nType;
							snsCommentNeedSend.referComment.nCreateTime = current.nCreateTime;
							snsCommentNeedSend.referComment.nSource = current.nSource;
							snsCommentNeedSend.referComment.nReplyCommentId = current.nReplyCommentId;
						}
					}
				}
			}
			return snsCommentNeedSend;
		}
	}
}
